﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark
{
    public partial class SanPhamYeuThich : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["User"]!=null)
            {
                KHACH_HANG kh = Session["User"] as KHACH_HANG;
                List<SAN_PHAM> ltSanPhamKM = SanPhamModels.LaySanPhamYeuThich(kh.ID);

                ListView1.DataSource = ltSanPhamKM;
                ListView1.DataBind();
            }
            else
            {
                Response.Redirect("/");
            }
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Label Rating = e.Item.FindControl("rating") as Label;
            Label LuotXem = e.Item.FindControl("LuotXem") as Label;
            SAN_PHAM h = e.Item.DataItem as SAN_PHAM;
            Rating.Text = Utils.GenerateRation(h.DANH_GIA.ToList());
            LuotXem.Text = h.DANH_GIA.Count().ToString();
            Image Image = e.Item.FindControl("SpecIcon") as Image;
            Image.ImageUrl = "images/products/" + h.HINH_SAN_PHAM.First().Hinh;
            
            if(h.NgayBatDau<=DateTime.Now && h.NgayKetThuc>=DateTime.Now)
            {
                Label oldPrice = e.Item.FindControl("oldPrice") as Label;
                Label newPrice = e.Item.FindControl("newPrice") as Label;
                oldPrice.Text = String.Format(System.Globalization.CultureInfo.InvariantCulture,"{0:0,0}", h.DonGia)+"<span class='sym'>đ</span>";
                newPrice.Text = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", h.DonGia - (h.DonGia * h.GiamGia) / 100) + "<span class='sym'>đ</span>";
            }
            else
            {
                Label newPrice = e.Item.FindControl("newPrice") as Label;
                newPrice.Text = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", h.DonGia) + "<span class='sym'>đ</span>";
            }
        }
    }
}